package com.YxyBackMange.Dto;



import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2017/4/15.
 */
public class QueryInfo {
    private String type;
    private String searchKey="%";
    private Integer pageSize=10;
    private Integer currentPage=1;
    private String filterName;//过滤的属性名称
    private String startValue;//过滤的开始值
    private String endValue;//过滤的结束值
    private String order="desc";//
    private String sort="id";
    private Map<String,String> map=new HashMap<String, String>();//筛选相关参数
    public QueryInfo() {

    }

    public String getFilterName() throws Exception {
        if((null!=type&&type.equals("filter"))&&null==filterName||"".equals(filterName))
        {
            if(null==filterName||filterName.equals(""))

            throw new Exception("过滤的属性名称不能为空");
        }
        return filterName;
    }

    public void setFilterName(String filterName) {
        this.filterName = CamelToLine.camelToUnderline(filterName);
    }

    public String getStartValue() throws Exception {
        if((null!=type&&type.equals("filter"))&&null==startValue||"".equals(startValue))
        {
            throw new Exception("过滤的属性开始值不能为空");
        }
        return startValue;
    }

    public void setStartValue(String startValue) {
        this.startValue = startValue;
    }

    public String getEndValue() throws Exception {
        if((null!=type&&type.equals("filter"))&&null==endValue||"".equals(endValue))
        {
            throw new Exception("过滤的属性结束值不能为空");
        }
        return endValue;
    }

    public void setEndValue(String endValue) {
        this.endValue = endValue;
    }

    public String getOrder() {
        return sort+" "+order;
    }

    public void setOrder(String order) {

        this.order = CamelToLine.camelToUnderline(order);
    }

    public String getSort() {
        return sort;
    }

    public void setSort(String sort) {
        this.sort = CamelToLine.camelToUnderline(sort);
    }

    public Integer getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(Integer currentPage) {
        this.currentPage = currentPage;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getSearchKey() {
        return searchKey;
    }

    public void setSearchKey(String searchKey) {
        this.searchKey = searchKey+"%";
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }


    public Map<String, String> getMap() {
        return map;
    }

    public void setMap(Map<String, String> map) {

        this.map = map;
    }

    @Override
    public String toString() {
        return "QueryInfo{" +
                "type='" + type + '\'' +
                ", searchKey='" + searchKey + '\'' +
                ", pageSize=" + pageSize +
                ", currentPage=" + currentPage +
                ", filterName='" + filterName + '\'' +
                ", startValue='" + startValue + '\'' +
                ", endValue='" + endValue + '\'' +
                ", order='" + order + '\'' +
                ", sort='" + sort + '\'' +
                ", map=" + map +
                '}';
    }
}

class CamelToLine {
    public static final char UNDERLINE = '_';

    public static String camelToUnderline(String param) {
        if (param == null || "".equals(param.trim())) {
            return "";
        }
        int len = param.length();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c = param.charAt(i);
            if (Character.isUpperCase(c)) {
                sb.append(UNDERLINE);
                sb.append(Character.toLowerCase(c));
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    public static String underlineToCamel(String param) {
        if (param == null || "".equals(param.trim())) {
            return "";
        }
        int len = param.length();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c = param.charAt(i);
            if (c == UNDERLINE) {
                if (++i < len) {
                    sb.append(Character.toUpperCase(param.charAt(i)));
                }
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    public static String underlineToCamel2(String param) {
        if (param == null || "".equals(param.trim())) {
            return "";
        }
        StringBuilder sb = new StringBuilder(param);
        Matcher mc = Pattern.compile("_").matcher(param);
        int i = 0;
        while (mc.find()) {
            int position = mc.end() - (i++);
            //String.valueOf(Character.toUpperCase(sb.charAt(position)));
            sb.replace(position - 1, position + 1, sb.substring(position, position + 1).toUpperCase());
        }
        return sb.toString();
    }
}
